package com.zyh.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyh.entity.Category;
import com.zyh.entity.ResultEntity;
import com.zyh.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品三级分类 前端控制器
 * </p>
 *
 * @author zhang
 * @since 2022-05-11
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    //根据父类别的编号查询该类别下面所有的子类别
    @GetMapping("/selectChildrenCategory/{categoryId}")
    public ResultEntity<List<Category>> selectChildrenCategory(@PathVariable("categoryId") Integer categoryId){
        return ResultEntity.ok(categoryService.list(new QueryWrapper<Category>().eq("parent_id", categoryId)));
    }

    //添加类别
    @PostMapping("/add")
    public ResultEntity add(@RequestBody Category category){
        return categoryService.save(category)==true ? ResultEntity.ok() : ResultEntity.error();
    }

    //删除类别
    @DeleteMapping("/delete/{categoryId}")
    public ResultEntity delete(@PathVariable("categoryId") Integer categoryId){
        return categoryService.removeById(categoryId)==true ? ResultEntity.ok() : ResultEntity.error();
    }

    //根据Id查询类别信息
    @GetMapping("/selectOne/{categoryId}")
    public ResultEntity<Category> selectOne(@PathVariable("categoryId") Integer categoryId){
        return ResultEntity.ok(categoryService.getById(categoryId));
    }

    //修改类别
    @PutMapping("/update")
    public ResultEntity update(@RequestBody Category category){
        return categoryService.updateById(category)==true ? ResultEntity.ok() : ResultEntity.error();
    }

    //查询整个类别数
    @GetMapping("/selectTree")
    public ResultEntity<List<Category>> selectTree(){
        QueryWrapper<Category> wrapper = new QueryWrapper<Category>().last("limit 50");
        return ResultEntity.ok(categoryService.selectMenu(categoryService.list(wrapper)));
    }
}

